package cn.edu.sgu.www.mhxysy.service.role.impl;

import cn.edu.sgu.www.mhxysy.dto.role.RoleConfigSchemaDTO;
import cn.edu.sgu.www.mhxysy.dto.role.RoleConfigSchemaSaveDTO;
import cn.edu.sgu.www.mhxysy.entity.role.*;
import cn.edu.sgu.www.mhxysy.enums.OpenStatus;
import cn.edu.sgu.www.mhxysy.mapper.role.*;
import cn.edu.sgu.www.mhxysy.service.role.RoleConfigSchemaService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Service
public class RoleConfigSchemaServiceImpl implements RoleConfigSchemaService {

    private final RoleConfigSchemaMapper roleConfigSchemaMapper;
    private final RoleConfigJiadianMapper roleConfigJiadianMapper;
    private final RoleConfigJingmaiMapper roleConfigJingmaiMapper;
    private final RoleConfigPotencyMapper roleConfigPotencyMapper;
    private final RoleConfigChongwuMapper roleConfigChongwuMapper;
    private final RoleConfigEquipmentMapper roleConfigEquipmentMapper;

    @Autowired
    public RoleConfigSchemaServiceImpl(
            RoleConfigSchemaMapper roleConfigSchemaMapper,
            RoleConfigJiadianMapper roleConfigJiadianMapper,
            RoleConfigJingmaiMapper roleConfigJingmaiMapper,
            RoleConfigPotencyMapper roleConfigPotencyMapper,
            RoleConfigChongwuMapper roleConfigChongwuMapper,
            RoleConfigEquipmentMapper roleConfigEquipmentMapper) {
        this.roleConfigSchemaMapper = roleConfigSchemaMapper;
        this.roleConfigJiadianMapper = roleConfigJiadianMapper;
        this.roleConfigJingmaiMapper = roleConfigJingmaiMapper;
        this.roleConfigPotencyMapper = roleConfigPotencyMapper;
        this.roleConfigChongwuMapper = roleConfigChongwuMapper;
        this.roleConfigEquipmentMapper = roleConfigEquipmentMapper;
    }

    @Override
    public void save(RoleConfigSchemaSaveDTO roleConfigSchemaSaveDTO) {
        // 得到角色ID
        String roleId = roleConfigSchemaSaveDTO.getRoleId();
        // 得到角色配置方案ID
        Integer configId = roleConfigSchemaSaveDTO.getConfigId();

        // 构建查询条件
        Map<String, Object> columnMap = new HashMap<>();

        columnMap.put("role_id", roleId);
        columnMap.put("config_id", configId);

        // 1、保存角色配置-加点
        String jiadianId = roleConfigSchemaSaveDTO.getJiadianId();
        List<RoleConfigJiadian> jiadians = roleConfigJiadianMapper.selectByMap(columnMap);

        if (jiadians.isEmpty()) {
            RoleConfigJiadian roleConfigJiadian = new RoleConfigJiadian();

            roleConfigJiadian.setId(null);
            roleConfigJiadian.setRoleId(roleId);
            roleConfigJiadian.setConfigId(configId);
            roleConfigJiadian.setJiadianId(jiadianId);

            roleConfigJiadianMapper.insert(roleConfigJiadian);
        } else {
            UpdateWrapper<RoleConfigJiadian> updateWrapper = new UpdateWrapper<>();

            updateWrapper.set("jiadian_id", jiadianId);

            updateWrapper.eq("role_id", roleId);
            updateWrapper.eq("config_id", configId);
            updateWrapper.ne("jiadian_id", jiadianId);

            roleConfigJiadianMapper.update(updateWrapper.getEntity(), updateWrapper);
        }

        // 2、保存角色配置-潜能
        Integer potencyId = roleConfigSchemaSaveDTO.getPotencyId();
        List<RoleConfigPotency> potencies = roleConfigPotencyMapper.selectByMap(columnMap);

        if (potencies.isEmpty()) {
            RoleConfigPotency roleConfigPotency = new RoleConfigPotency();

            roleConfigPotency.setId(null);
            roleConfigPotency.setRoleId(roleId);
            roleConfigPotency.setConfigId(configId);
            roleConfigPotency.setPotencyId(potencyId);

            roleConfigPotencyMapper.insert(roleConfigPotency);
        } else {
            UpdateWrapper<RoleConfigPotency> updateWrapper = new UpdateWrapper<>();

            updateWrapper.set("potency_id", potencyId);

            updateWrapper.eq("role_id", roleId);
            updateWrapper.eq("config_id", configId);
            updateWrapper.ne("potency_id", potencyId);

            roleConfigPotencyMapper.update(updateWrapper.getEntity(), updateWrapper);
        }

        // 3、保存角色配置-经脉
        Integer jingmaiId = roleConfigSchemaSaveDTO.getJingmaiId();
        List<RoleConfigJingmai> jingmais = roleConfigJingmaiMapper.selectByMap(columnMap);

        if (jingmais.isEmpty()) {
            RoleConfigJingmai roleConfigJingmai = new RoleConfigJingmai();

            roleConfigJingmai.setId(null);
            roleConfigJingmai.setRoleId(roleId);
            roleConfigJingmai.setConfigId(configId);
            roleConfigJingmai.setJingmaiId(jingmaiId);

            roleConfigJingmaiMapper.insert(roleConfigJingmai);
        } else {
            UpdateWrapper<RoleConfigJingmai> updateWrapper = new UpdateWrapper<>();

            updateWrapper.set("jingmai_id", jingmaiId);

            updateWrapper.eq("role_id", roleId);
            updateWrapper.eq("config_id", configId);
            updateWrapper.ne("jingmai_id", jingmaiId);

            roleConfigJingmaiMapper.update(updateWrapper.getEntity(), updateWrapper);
        }

        // 4、保存角色配置-宠物
        String chongwuId = roleConfigSchemaSaveDTO.getChongwuId();
        List<RoleConfigChongwu> chongwus = roleConfigChongwuMapper.selectByMap(columnMap);

        if (chongwus.isEmpty()) {
            RoleConfigChongwu roleConfigChongwu = new RoleConfigChongwu();

            roleConfigChongwu.setId(null);
            roleConfigChongwu.setRoleId(roleId);
            roleConfigChongwu.setConfigId(configId);
            roleConfigChongwu.setChongwuId(chongwuId);

            roleConfigChongwuMapper.insert(roleConfigChongwu);
        } else {
            UpdateWrapper<RoleConfigChongwu> updateWrapper = new UpdateWrapper<>();

            updateWrapper.set("chongwu_id", chongwuId);

            updateWrapper.eq("role_id", roleId);
            updateWrapper.eq("config_id", configId);
            updateWrapper.ne("chongwu_id", chongwuId);

            roleConfigChongwuMapper.update(updateWrapper.getEntity(), updateWrapper);
        }

        // 5、保存角色配置-装备
        Integer equipmentId = roleConfigSchemaSaveDTO.getEquipmentId();
        List<RoleConfigEquipment> equipments = roleConfigEquipmentMapper.selectByMap(columnMap);

        if (equipments.isEmpty()) {
            RoleConfigEquipment roleConfigEquipment = new RoleConfigEquipment();

            roleConfigEquipment.setId(null);
            roleConfigEquipment.setRoleId(roleId);
            roleConfigEquipment.setConfigId(configId);
            roleConfigEquipment.setEquipmentId(equipmentId);

            roleConfigEquipmentMapper.insert(roleConfigEquipment);
        } else {
            UpdateWrapper<RoleConfigEquipment> updateWrapper = new UpdateWrapper<>();

            updateWrapper.set("equipment_id", equipmentId);

            updateWrapper.eq("role_id", roleId);
            updateWrapper.eq("config_id", configId);
            updateWrapper.ne("equipment_id", equipmentId);

            roleConfigEquipmentMapper.update(updateWrapper.getEntity(), updateWrapper);
        }
    }

    @Override
    public void insert(RoleConfigSchemaDTO roleConfigSchemaDTO) {
        RoleConfigSchema roleConfigSchema = new RoleConfigSchema();

        roleConfigSchema.setId(null);
        roleConfigSchema.setName(roleConfigSchemaDTO.getName());
        roleConfigSchema.setRoleId(roleConfigSchemaDTO.getRoleId());
        roleConfigSchema.setOpenStatus(OpenStatus.OPEN.getValue());

        roleConfigSchemaMapper.insert(roleConfigSchema);
    }

    @Override
    public void updateNameById(Integer id, String name) {
        UpdateWrapper<RoleConfigSchema> updateWrapper = new UpdateWrapper<>();

        updateWrapper.eq("id", id);
        updateWrapper.set("name", name);

        roleConfigSchemaMapper.update(updateWrapper.getEntity(), updateWrapper);
    }

    @Override
    public RoleConfigSchema selectOpenSchema(String roleId) {
        QueryWrapper<RoleConfigSchema> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("role_id", roleId);
        queryWrapper.eq("open_status", OpenStatus.OPEN.getValue());

        return roleConfigSchemaMapper.selectOne(queryWrapper);
    }

    @Override
    public List<RoleConfigSchema> selectByRoleId(String roleId) {
        QueryWrapper<RoleConfigSchema> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("role_id", roleId);

        return roleConfigSchemaMapper.selectList(queryWrapper);
    }

}